package com.cc.pms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cc.pms.mapper.TagMapper;
import com.cc.pms.domain.Tag;
import com.cc.pms.domain.vo.TagVo;
import com.cc.common.core.domain.R;
import com.cc.common.core.page.PageQuery;
import com.cc.common.core.page.PageVo;
import com.cc.pms.service.ITagService;
import com.cc.pms.service.ITeamTagService;
import com.cc.pms.service.IUserTagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements ITagService {


    @Resource
    private ITeamTagService iTeamTagService;

    @Resource
    private IUserTagService iUserTagService;

    @Override
    public PageVo<TagVo> pageVo(Tag tag, PageQuery query) {
        if (query == null) {
            query = new PageQuery();
        }
        Page<TagVo> page = new Page<>(query.getPageNum(), query.getPageSize());
        Page<TagVo> tagVoPage = baseMapper.queryTagVoPage(page, tag);
        return PageVo.of(tagVoPage.getRecords() , tagVoPage.getTotal());
    }

    @Override
    public R<Long> create(Tag tag) {
        if (tag == null || tag.getName() == null || tag.getName().trim().isEmpty() || tag.getTypeId() == null) {
            return R.fail(-1, "请求数据错误");
        }
        // 检查标签名是否重复
        if (baseMapper.selectCount(Wrappers.<Tag>lambdaQuery().eq(Tag::getName, tag.getName()).eq(Tag::getTypeId, tag.getTypeId())) > 0) {
            return R.fail(-1, "类型标签中已存在相同标签名称");
        }
        save(tag);
        return R.ok(tag.getId());
    }

    @Override
    public R<Boolean> update(Tag tag) {
        if (tag == null || tag.getId() == null || tag.getName() == null || tag.getName().trim().isEmpty()) {
           return R.fail(-1 , "请求数据错误");
        }
        Tag existingTag = baseMapper.selectById(tag.getId());
        if (existingTag == null) {
            return R.fail(-1 , "数据不存在,无法修改");
        }
        // 检查新标签名是否与旧名称不同且是否重复
        if (!tag.getName().equals(existingTag.getName())) {
            if (baseMapper.selectCount(Wrappers.<Tag>lambdaQuery().eq(Tag::getName, tag.getName()).eq(Tag::getTypeId, tag.getTypeId()).ne(tag.getId() != null , Tag::getId , tag.getId())) > 0) {
                return R.fail(-1 , "类型标签中已存在相同标签名称["+ tag.getName() +"],无法修改");
            }
        }
        return R.ok(updateById(tag));
    }

    @Override
    public R<Boolean> delete(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return R.fail(-1 , "请求数据有误");
        }
        boolean succ = removeByIds(Arrays.asList(ids));
        if (succ){
            iTeamTagService.removeByTagIds(ids);
            iUserTagService.removeByTagIds(ids);
        }
        return succ ? R.ok() : R.fail(-1 ,"删除数据失败");
    }

    @Override
    public Tag getById(Long id) {
        if (id == null) {
            return null;
        }
        return baseMapper.selectById(id);
    }

    @Override
    public List<Tag> listAll() {
        return baseMapper.selectList(Wrappers.emptyWrapper());
    }

    @Override
    public List<Tag> listByTypeId(Long typeId) {
        if (typeId == null) {
            return null;
        }
        return baseMapper.selectList(Wrappers.<Tag>lambdaQuery().eq(Tag::getTypeId, typeId));
    }
}